package com.aw.baselib.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import com.aw.baselib.base.AwBaseApplication
import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException

/**
 * Created by hzw.
 */
object NetUtil {
    val isOpenNetwork: Boolean
        /**
         * 对网络连接状态进行判断
         *
         * @return true, 可用； false， 不可用
         */
        get() {
            val connManager = AwBaseApplication.instance?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connManager.activeNetworkInfo
            if (networkInfo != null) {
                //2.获取当前网络连接的类型信息
                val networkType = networkInfo.type
                if (ConnectivityManager.TYPE_WIFI == networkType) {
                } else if (ConnectivityManager.TYPE_MOBILE == networkType) {
                }
                return connManager.activeNetworkInfo!!.isAvailable
            }
            return false
        }

    // 判断当前是否使用的是 WIFI网络
    fun isWifiActive(icontext: Context): Boolean {
        val context = icontext.applicationContext
        val connectivity = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info: Array<NetworkInfo>
        if (connectivity != null) {
            info = connectivity.allNetworkInfo
            if (info != null) {
                for (i in info.indices) {
                    if (info[i].typeName == "WIFI" && info[i].isConnected) {
                        return true
                    }
                }
            }
        }
        return false
    }

    fun getIPAddress(context: Context): String? {
        val info = (context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
        if (info != null && info.isConnected) {
            if (info.type == ConnectivityManager.TYPE_MOBILE) { //当前使用2G/3G/4G网络
                try {
                    //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                    val en = NetworkInterface.getNetworkInterfaces()
                    while (en.hasMoreElements()) {
                        val intf = en.nextElement()
                        val enumIpAddr = intf.inetAddresses
                        while (enumIpAddr.hasMoreElements()) {
                            val inetAddress = enumIpAddr.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                                return inetAddress.getHostAddress()
                            }
                        }
                    }
                } catch (e: SocketException) {
                    e.printStackTrace()
                }
            } else if (info.type == ConnectivityManager.TYPE_WIFI) { //当前使用无线网络
                val wifiManager =
                    context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                val wifiInfo = wifiManager.connectionInfo
                return intIP2StringIP(wifiInfo.ipAddress)
            }
        } else {
            //当前无网络连接,请在设置中打开网络
        }
        return null
    }

    /**
     * 将得到的int类型的IP转换为String类型
     *
     * @param ip
     * @return
     */
    fun intIP2StringIP(ip: Int): String {
        return (ip and 0xFF).toString() + "." +
                (ip shr 8 and 0xFF) + "." +
                (ip shr 16 and 0xFF) + "." +
                (ip shr 24 and 0xFF)
    }
}